export default class BBB {
    constructor() {}
    /***
     * 创建扫描实体
     * opt.v
     * opt.positions
     * positions{lat,lon,alt}
     */
    createScan(opt) {
        let _self = this;
        let viewer = opt.viewer;
        let length = opt.positions.alt;
        _self.modelMatrix = opt.modelMatrix;
        // 4 创建雷达放射波
        var cylinderGeometry = new Cesium.CylinderGeometry({
            // 4.1 先创建Geometry
            length: length,
            topRadius: 0.0,
            bottomRadius: length * 0.5,
            //vertexFormat : Cesium.PerInstanceColorAppearance.VERTEX_FORMAT
            vertexFormat: Cesium.MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat
        });
        var redCone = new Cesium.GeometryInstance({
            // 4.2 创建GeometryInstance
            geometry: cylinderGeometry,
            modelMatrix: _self.modelMatrix
            // attributes : {
            //     color : Cesium.ColorGeometryInstanceAttribute.fromColor(Cesium.Color.RED)
            // }
        });
        var radar = viewer.scene.primitives.add(
            new Cesium.Primitive({
                // 4.3 创建Primitive
                geometryInstances: [redCone],
                // appearance : new Cesium.PerInstanceColorAppearance({
                //     closed : true,
                //     translucent: false
                // })
                appearance: new Cesium.MaterialAppearance({
                    // 贴图像纹理
                    // material: Cesium.Material.fromType('Image', {
                    //     image: '../../SampleData/models/CesiumBalloon/CesiumBalloonPrint_singleDot.png'
                    // }),

                    // 贴棋盘纹理
                    // material: Cesium.Material.fromType('Checkerboard'),

                    // 自定义纹理
                    material: new Cesium.Material({
                        fabric: {
                            type: "VtxfShader1",
                            uniforms: {
                                color: new Cesium.Color(0.2, 1.0, 0.0, 1.0),
                                repeat: 30.0,
                                offset: 0.0,
                                thickness: 0.3
                            },
                            source: `
                            uniform vec4 color;
                            uniform float repeat;
                            uniform float offset;
                            uniform float thickness;

                            czm_material czm_getMaterial(czm_materialInput materialInput)
                            {
                                czm_material material = czm_getDefaultMaterial(materialInput);
                                float sp = 1.0/repeat;
                                vec2 st = materialInput.st;
                                float dis = distance(st, vec2(0.5));
                                float m = mod(dis + offset, sp);
                                float a = step(sp*(1.0-thickness), m);

                                material.diffuse = color.rgb;
                                material.alpha = a * color.a;

                                return material;
                            }
                        `
                        },
                        translucent: false
                    }),
                    faceForward: false, // 当绘制的三角面片法向不能朝向视点时，自动翻转法向，从而避免法向计算后发黑等问题
                    closed: true // 是否为封闭体，实际上执行的是是否进行背面裁剪
                })
            })
        );

        // 5 动态修改雷达材质中的offset变量，从而实现动态效果。
        viewer.scene.preUpdate.addEventListener(function() {
            var offset = radar.appearance.material.uniforms.offset;
            offset -= 0.001;
            if (offset > 1.0) {
                offset = 0.0;
            }
            radar.appearance.material.uniforms.offset = offset;
        });
        let _update = function() {
            return _self.modelMatrix;
        };
        radar.modelMatrix = new Cesium.CallbackProperty(_update, true);
        return radar;
    }

    /**
     * 动态创建直线
     * @param {*} positions
     */
    createLine(opt) {
        this.options = {
            name: "直线",
            polyline: {
                show: true,
                positions: [],
                material: opt.material == undefined ? Cesium.Color.CHARTREUSE : opt.material,
                width: opt.width == undefined ? 5 : opt.width,
                clampToGround: opt.clampToGround == undefined ? false : true //贴地
            }
        };
        this.positions = opt.positions;
        //实时更新polyline.positions
        var _self = this;
        var _update = function() {
            return _self.positions;
        };
        this.options.polyline.positions = new Cesium.CallbackProperty(_update, false);
    }

    /**
     * 动态绑定柱体
     * 应用于 卫星过境 无人机
     * 扫描
     * @param {*} obj
     */
    dynamicCylinder(obj){
        let c = obj.cylinder;
        this.options = {
            cylinder: {
                HeightReference: Cesium.HeightReference.RELATIVE_TO_GROUND, //表示相对于地形的位置。
                length: c.length,     //长度
                topRadius:0,    //顶点半径
                bottomRadius:c.bottomRadius,  //底部半径
                material:new Cesium.Color(0, 1, 1, .4),
                slices:c.slices
            }
        }
        this.positions = obj.positions;
        this.entity = obj.entity;
        this.v = obj.v;
        var _self = this;
        var _update = function(){
            var positions = _self.entity.position.getValue(_self.v.clock.currentTime);
            var cartographic = _self.v.scene.globe.ellipsoid.cartesianToCartographic(positions);
            var lat = Cesium.Math.toDegrees(cartographic.latitude)
                , lng = Cesium.Math.toDegrees(cartographic.longitude)
                ,hei = parseFloat(cartographic.height/4);
            return Cesium.Cartesian3.fromDegrees(lng, lat,0);
        };
        var _length = function(){
            var positions = _self.entity.position.getValue(_self.v.clock.currentTime);
            var cartographic = _self.v.scene.globe.ellipsoid.cartesianToCartographic(positions);
            return cartographic.height * 2;
        }
        this.options.position = new Cesium.CallbackProperty(_update,false);
        this.options.cylinder.length = new Cesium.CallbackProperty(_length,false);
    }
}
